package org.formal.system.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.formal.common.controller.BaseController;
import org.formal.common.data.ResultData;
import org.formal.common.utils.WordUtils;
import org.formal.system.entity.FileEntity;
import org.formal.system.entity.RoleEntity;
import org.formal.system.entity.UserEntity;
import org.formal.system.service.FileService;
import org.formal.system.service.RoleService;
import org.formal.system.shiro.manager.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value="file")
public class FileController extends BaseController<FileEntity>{
	
	@Autowired
	private FileService fileService;
	
	@Autowired
	private RoleService roleService;
	
	/**
	 * 文件上传
	 * @param file
	 * @param parentId
	 * @return
	 */
	@RequestMapping(value="/list/upload")
	public ResultData fileUpload(@RequestParam("file") MultipartFile file, String parentId) {
		ResultData resultData = null;
		try {
			this.fileService.saveFile(file, parentId);
			resultData = getResultData(true, "上传成功");
		} catch (Exception e) {
			resultData = getResultData(true, "上传失败");
		}
		return resultData;
	}
	
	/**
	 * 生成word
	 */
	@RequestMapping(value="/generate/word")
	public void generateWord(HttpServletResponse response) {
		UserEntity user = TokenManager.getToken();
		List<RoleEntity> roles = this.roleService.getRoleByUserId(user.getId());
		Map<String,Object> resourceMap = new HashMap<String,Object>(roles.size());
		for(RoleEntity role : roles) {
			List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
			int i = 0;
			while(i < 10) {
				i++;
				Map<String,Object> map = new HashMap<String,Object>(16);
				map.put("menuNo", 12);
				map.put("menuName", 222);
				list.add(map);
			}
			resourceMap.put(role.getId(), list);
		}
		Map<String,Object> dataMap = new HashMap<String,Object>();
		dataMap.put("user", user);
		dataMap.put("roles", roles);
		dataMap.put("resourceMap", resourceMap);
		WordUtils.createWord(dataMap, "xml/用户信息.xml", "F:", user.getNickName()+"权限信息.docx");
		WordUtils.downFile("F:/"+user.getNickName()+"权限信息.docx", response);
	}
}
